<?php
class PublicAction extends Action{
	
	
	//注册
	public function register(){
		$this->display("register");
	}
	
	public function doregister(){
		
// 		dump($_REQUEST);
// 		dump($_REQUEST["email"] == "" );exit;
		$password = $_REQUEST["password"];
		$confirmpassword = $_REQUEST["confirmpassword"];
		if($password == ""  ){
			$data=array("massage"=>"密码不能为空","url"=>"partner/Public/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
		if($_REQUEST["name"] == "" ){
			$data=array("massage"=>"公司名称不能为空","url"=>"partner/Public/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
		if($_REQUEST["email"] == "" ){
			$data=array("massage"=>"邮箱不能空","url"=>"partner/Public/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
		if($_REQUEST["contact"] == "" ){
			$data=array("massage"=>"联系人不能为空","url"=>"partner/Public/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
		if($_REQUEST["phone"] == "" ){
			$data=array("massage"=>"联系电话不能为空","url"=>"partner/Public/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
		if($password != $confirmpassword){
			$data=array("massage"=>"两次密码不一致","url"=>"partner/User/register","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		$partner = M("Partner");
		$partner->email = $_REQUEST["email"];
		$partner->name = $_REQUEST["name"];
		$partner->address = $_REQUEST["address"];
		$partner->contact = $_REQUEST["contact"];
		$partner->qq = $_REQUEST["qq"];
		$partner->phone = $_REQUEST["phone"];
		$partner->password = md5($password);
		if($partner->add()){
			$data=array("massage"=>"注册成功","url"=>"partner/Index/index","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
	}
	//登录
	public function login(){
		$this->display();
	}
	
	public function dologin(){
		
		$email = $_REQUEST["email"];
		$password = $_REQUEST["password"];
		$partner = M("Partner")->where("email = '$email'")->find();
		
		
		if(count($partner)<=0){
			$data=array("massage"=>"该账户不存在","url"=>"partner/Public/login","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		if($partner["password"] == md5($password)){
			$_SESSION["pid"] = $partner["pid"];
			$_SESSION["name"] = $partner["name"];
			setcookie("pid",$partner["pid"],3600);
			
			$data=array("massage"=>"登录成功","url"=>"partner/Index/index","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}else{
			$data=array("massage"=>"密码错误","url"=>"partner/Public/login","place"=>"login");
			$this->redirect("partner/Public/redirectHtml",$data);
		}
	}
	public function redirectHtml(){
		//dump($_REQUEST);exit;
		$jumpUrl = explode("/", $_REQUEST["url"]);
		$place = isset($_REQUEST["place"])?$_REQUEST["place"]:"";
		$type = isset($_REQUEST["type"])?$_REQUEST["type"]:"";
		
		if($type != ""){
			$type = "&type=".$type;
		}
		$url = C("__URL__")."app=".$jumpUrl[0]."&mod=".$jumpUrl[1]."&act=".$jumpUrl[2].$type;
		$massage = $_REQUEST["massage"];
		$second = 2;
		$this->assign("url",$url);
		$this->assign("massage",$massage);
		$this->assign("second",$second);
		if($place == "login"){
			$this->display("login_result");exit;
		}
		$this->display("result");
	}
	
	public function logout(){
		unset($_SERVER["pid"]);
		unset($_SERVER["name"]);
		session_unset();
		setcookie("pid",null);	
		$this->redirect("partner/Public/login");
	}
}